Built-In Functions

In mathematics a function is something that takes input (often called the arguments or parameters of the function) and returns output (also called the return value). Examples are the trigonometric functions such as sine and cosine and the exponential function. A function in a programming language is similar in that it takes inputs and returns an output. If you generalize a function to the form y=f(x), then the input(s) to the function are the independent variable, x, and the return value of the function is the dependent variable, y. In this example, f is the name of the function. Parentheses are used to group the list of input values to a function. Commas are used to separate one input value from the next in the list of input values. This is a common technique in most programming languages. In general, functions can have any number of input values, but only return one value.

Matlab has an extensive library of built-in functions available for you to use from the command line window or in any code that you write. There are hundreds of functions available. We will only see a small percentage of the functions that Matlab has pre-defined for your use. The trigonometric functions, sin(), cos(), tan(), etc. are available as well as some functions for operations that may not be obvious at first, like sqrt() which calculates the square root of an input value.

Most of the common mathematical functions you are familiar with are available in Matlab. These include functions to compute things like the absolute value abs(), natural logarithms log() and base-10 logarithms log10(), exponentials exp(), and rounding round(), floor(), ceil(). These functions work on matrices (arrays and vectors) as well as on single values (scalars): If you have cos(x) and x is a matrix, then Matlab simply performs the cosine function on each element in x. In other words, the cos() function - as well as the other mathematical functions - operates element-wise.

Using built-in functions is much easier and less error prone than writing your own code to calculate the same value. This is because the built-in functions have been written, reviewed, refactored, and tested extensively to ensure that they are correct and have the highest degree of efficiency in calculating the desired value. Read the help files for the functions that you intend to use to ensure that they perform the calculation that you desire. If they do not, you will need to define your own function as described in a later lesson in this module.

Another important use of functions is to simplify the writing of code. For example, it would be very difficult to determine what a block of code does if included in the code were the Matlab commands that computed the sine of x instead of just the command sin(x). Another Matlab function is isprime() which returns either 1 if it is a prime integer or 0 if it is not a prime integer.

You call functions in the command window by typing the name of the function and the inputs to the function enclosed in parantheses and then press enter.

A function value to compute The syntax to call the function from the command window

""

>> sin( pi/2 )
cosine of x (x must have been assigned a value previously) >> cos( x )
returns 1 if integer 1347 is a prime number >> isprime( 1347 )
returns 1 if integer stored in myValue is a prime number >> isprime( myValue )

You can use Matlab's other operators on the results of a function call. For example, 2*sin(x) will produce the value of two times the result of sine of x.

When typing function names you must use the exact spelling and capitalization. Also, the order of the input values (arguments) must match the order that the values have been defined. If there are multiple inputs to a function, be sure to review the online help to ensure that you are providing them in the correct order.

Practice using each of the functions mentioned to be sure that you understand how to call each function.

Matrix Generation Functions

Matlab has several matrix generation functions that you will probably find quite useful as well. Two of the most useful of these are zeros() and ones(). The command zeros(nRows,nCols) creates an nRows x nCols matrix with all of the elements set to 0. The function ones works similarly except that all matrix entries are set to the value 1.